import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import organizationManagement from './modules/organizationManagement'
import workingArea from './modules/workingArea'
import transportFee from './modules/transportFee'
import carType from './modules/carType'
import carList from './modules/carList'
import returnCar from './modules/returnCar'
import deliveryman from './modules/deliveryman'
import driver from './modules/driver'
import workforceManagement from './modules/workforceManagement'
import orderForm from './modules/orderForm'
import waybill from './modules/waybill'
import transportTask from './modules/transportTask'
import lineManagement from './modules/lineManagement'
import pickup from './modules/pickup'
import delivery from './modules/delivery'
import orderDetail from './modules/orderDetail'
import waybillDetail from './modules/waybillDetail'
import driverson from './modules/driverson'
import set from './modules/set'
import wek from './modules/wek'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  { // 登录
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  { // 404
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  { // 主页
    path: '/',
    component: Layout,
    redirect: '/workbench',
    children: [{
      path: 'workbench',
      name: 'Workbench',
      component: () => import('@/views/workbench/index'),
      meta: { title: '工作台', icon: 'el-icon-price-tag' }
    }]
  },

  { // 基础数据管理
    path: '/dataManagement',
    component: Layout,
    redirect: '/organizationManagement',
    name: 'DataManagement',
    meta: { title: '基础数据管理', icon: 'el-icon-s-data' },
    children: [
      organizationManagement, // 机构管理
      workingArea, // 机构作业范围
      transportFee // 运费管理
    ]
  },

  { // 车辆管理
    path: '/car',
    component: Layout,
    redirect: '/carList',
    name: 'Car',
    meta: { title: '车辆管理', icon: 'el-icon-truck' },
    children: [
      carType, // 车型管理
      carList, // 车辆列表
      returnCar // 回车登记
    ]
  },

  { // 员工管理
    path: '/employee',
    component: Layout,
    redirect: '/deliveryman',
    name: 'Employee',
    meta: { title: '员工管理', icon: 'el-icon-user' },
    children: [
      deliveryman, // 快递员管理
      driver, // 司机管理
      workforceManagement, // 排班管理
      driverson,
      set, // 排班设置
      wek// 地图
    ]
  },

  { // 业务管理
    path: '/business',
    component: Layout,
    redirect: '/orderForm',
    name: 'Business',
    meta: { title: '业务管理', icon: 'el-icon-notebook-2' },
    children: [
      orderForm, // 订单管理
      waybill, // 运单管理
      orderDetail, // 订单详情
      waybillDetail // 运单详情
    ]
  },

  { // 调度管理
    path: '/dispatching',
    component: Layout,
    redirect: '/orderForm',
    name: 'Dispatching',
    meta: { title: '调度管理', icon: 'el-icon-set-up' },
    children: [
      transportTask, // 运输任务管理
      lineManagement, // 线路管理
      pickup, // 取件作业管理
      delivery // 派件作业管理
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
